/* entity */
import Field from '@model/entity/Field'
import { QualityRuleForm, QualityRuleOriginal } from '@model/entity/QualityRule'
/* enum */
import { FieldTypeMappingEnum, ProductTypeFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
/* service */
import { 
  convertFormQualityRuleFieldValueToQualityRuleRemoteData, 
  convertQualityRuleRemoteDataToFormQualityRuleFieldValue, 
  smoothQualityRuleField
} from '@service/QualityRuleService'
import { existsField, getFieldName, isQualityRuleField } from '@service/FieldService'
/* util */
import * as _ from 'lodash'
import { isObject } from '@src/util/type';
import { filterProperty } from '@src/util/lang/object'

// 产品类型批量编辑字段包含：质保期算时间、质保期时长、产品目录、产品简介
const ProductTypeBatchEditFieldNames: ProductTypeFieldNameMappingEnum[] = [
  ProductTypeFieldNameMappingEnum.ProductDescription,
  ProductTypeFieldNameMappingEnum.ProductCatalog,
  ProductTypeFieldNameMappingEnum.QualityStartTime,
  ProductTypeFieldNameMappingEnum.QualityTime
]

// 产品类型批量编辑不需要验证的字段列表 包含：质保期算时间、质保期时长、产品目录、产品简介
const ProductTypeBatchEditNotValidateFieldNames: ProductTypeFieldNameMappingEnum[] = []

/**
 * @description: 将服务端的数据转为表单数据
 * @param {Record} originData 原始数据
 * @return {Record}
 */
export function unpack(originData: Record<string, any>, fields: Field[]): Record<string, any> {
  // 质保起算时间
  let qualityStartDate: string = originData?.qualityStartDate || ''
  // 取字段默认值
  if (!qualityStartDate) {
    originData.qualityStartDate = getDefaultQualityStartDate(fields)
  }
  
  return {
    ...originData,
    [ProductTypeFieldNameMappingEnum.QualityRule]: convertQualityRuleRemoteDataToFormQualityRuleFieldValue(originData as QualityRuleOriginal, fields)
  }
}

/**
 * @description: 将表单数据数据转为服务端接收的数据
 * @param {Record} originData 原始数据
 * @return {Record}
 */
export function pack(formData: Record<string, any>, fields: Field[]): Record<string, any> {  
  let qualityRuleForm: QualityRuleForm = formData?.[ProductTypeFieldNameMappingEnum.QualityRule] || {}
  let data: Record<string, any> = {
    ...formData,
    ...convertFormQualityRuleFieldValueToQualityRuleRemoteData(qualityRuleForm, fields)
  }
  
  return (
    filterProperty(
      data,
      [
        ProductTypeFieldNameMappingEnum.QualityRule,
        ProductTypeFieldNameMappingEnum.QualityStartTime, 
        ProductTypeFieldNameMappingEnum.QualityTime,
        FieldTypeMappingEnum.QualityRule
      ]
    )
  )
  
}

/**
 * @description 获取允许产品类型批量编辑的字段
 * @param {Field[]} productTypeFields 产品类型字段列表
 * @return {Field[]} 允许产品类型批量编辑的字段列表
 */
export function getProductTypeBatchEditFields(productTypeFields: Field[]): Field[] {
  // 原产品类型字段列表
  let originProductTypeFields: Field[] = _.cloneDeep(productTypeFields)
  
  // 允许产品类型批量编辑的字段 (质保期算时间、质保期时长、产品目录、产品简介)
  let allowProductTypeBatchEditFields: Field[] = (
    originProductTypeFields
      .filter((productTypeField: Field) => {
        return (
          ProductTypeBatchEditFieldNames.includes(getFieldName(productTypeField) as ProductTypeFieldNameMappingEnum)
          || isQualityRuleField(productTypeField)
        )
      })
      .map((productTypeField: Field) => {
        if (getFieldName(productTypeField) == ProductTypeFieldNameMappingEnum.ProductCatalog) {
          productTypeField.formType = FieldTypeMappingEnum.Cascader
        }
        
        return productTypeField
      })
  )
  
  return (
    smoothQualityRuleField(allowProductTypeBatchEditFields)
  )
}

/**
 * @description 判断是否为产品类型不需要校验的字段
 * @param {Field} field 需要判断的字段
 * @return {Boolean} 是否不需要校验
 */
export function isProductTypeNotValidateField(field: Field): boolean {
  return ProductTypeBatchEditNotValidateFieldNames.includes(getFieldName(field) as ProductTypeFieldNameMappingEnum)
}

export function getDefaultQualityStartDate(fields: Field[]): string {  
  const qualityRuleField: Field | null = existsField(ProductTypeFieldNameMappingEnum.QualityRule, fields)
  return qualityRuleField?.defaultValue || ''
}

export default {
  unpack,
  pack,
  getProductTypeBatchEditFields,
  getDefaultQualityStartDate
}
